<template>
    <view class="globe_back">
        <view style="height: 16px"></view>
        <view class="back_fff br_16" style="margin: 0 16px 16px 16px">
            <!-- 收货人 -->
            <view class="p_32 flex align_items_center">
                <view class="flex align_items_center title">
                    <text class="f_28 cl_333A47">收货人</text>
                </view>
                <view class="flex align_items_center">
                    <input type="text" class="f_28" v-model="formData.name" placeholder="请填写收货人姓名" />
                </view>
            </view>
            <!-- 联系电话 -->
            <view class="p_32 flex align_items_center">
                <view class="flex align_items_center title">
                    <text class="f_28 cl_333A47">联系电话</text>
                </view>
                <view class="flex align_items_center">
                    <input type="text" class="f_28" v-model="formData.mobile" placeholder="请填写收货人联系电话" />
                </view>
            </view>
            <!-- 所在地区 -->
            <picker mode="region" @change="bindRegionChange">
                <view class="p_32 flex align_items_center">
                    <view class="flex align_items_center title">
                        <text class="f_28 cl_333A47">所在地区</text>
                    </view>
                    <view class="flex align_items_center justify_content_between" style="width: 80%">
                        <input type="text" class="f_28" disabled v-model="formData.fullRegion" placeholder="请选择收货所在地区" />
                        <image class="img_32" src="https://jiuyuan.smomotbo.com/static/imgs/query-info/next-icon.png" mode=""></image>
                    </view>
                </view>
            </picker>
            <!-- 详细地址 -->
            <view class="p_32 flex align_items_center">
                <view class="flex align_items_center title">
                    <text class="f_28 cl_333A47">详细地址</text>
                </view>
                <view class="flex align_items_center" style="width: 80%">
                    <input type="text" class="f_28" style="width: 100%" v-model="formData.address" placeholder="请填写收货人详细地址" />
                </view>
            </view>
        </view>
        <view class="addBtn back_3377FF br_24 cl_fff" @click="submit">保存</view>
    </view>
</template>

<script>
// import { saveAddress, memberInfo } from '@/api';
export default {
    data() {
        return {
            formData: {
                id: '',
                name: '',
                mobile: '',
                latitude: '',
                longitude: '',
                address: '',
                fullAddress: '',
                regionCode: [],
                postcode: '',
                regions: [],
                fullRegion: '',
                isDefault: false
            },
            region: [],
            customItem: '',
			type:0,
			id:0,
        };
    },
	onLoad(e) {
		if(e.id){
			this.$.ajax(1, "POST", "user.address/one_address", {
				id:e.id
			}, (res) => {
				if(res.code==200){
					console.log(res)
					this.formData.name=res.data.real_name,
					this.formData.mobile=res.data.phone,
					this.formData.regions=[res.data.province,res.data.city,res.data.district],
					this.formData.address=res.data.detail,
					this.formData.fullRegion=res.data.province+'-'+res.data.city+'-'+res.data.district
					console.log(this.formData)
				}else{
					this.$.ti_shi(res.msg)
				}
			})
		    this.type=e.type
			this.id=e.id
		}
	},
    // onShow() {
    //     this.formData = this.$current_address;
    // },
    methods: {
        bindRegionChange: function (e) {
            console.log('picker发送选择改变，携带值为', e.detail);
            this.formData.regionCode = e.detail.code;
            this.formData.regions = e.detail.value;
            this.formData.postcode = e.detail.postcode;
            this.formData.fullRegion = e.detail.value.join('-');
            console.log(111);
        },
        submit() {
          if(this.type==1){
			  this.$.ajax(1, "POST", "user.address/edit", {
			  	real_name: this.formData.name,
			  	phone: this.formData.mobile,
			  	province: this.formData.regions[0],
			  	city: this.formData.regions[1],
			  	district: this.formData.regions[2],
			  	id: this.id,
			  	detail: this.formData.address,
			  }, (res) => {
			  	if(res.code==200){
			  		this.$.ti_shi(res.msg)
			  		this.shows=false
			  		setTimeout(() => {
			  		     uni.navigateBack({
			  				 delta:1
			  			 })
			  		}, 1000);
			  	}else{
			  		this.$.ti_shi(res.msg)
			  	}
			  })
		  }else{
			  this.$.ajax(1, "POST", "user.address/add_edit", {
			  	real_name: this.formData.name,
			  	phone: this.formData.mobile,
			  	province: this.formData.regions[0],
			  	city: this.formData.regions[1],
			  	district: this.formData.regions[2],
			  	is_default: 0,
			  	detail: this.formData.address,
			  }, (res) => {
			  	if(res.code==200){
			  		this.$.ti_shi(res.msg)
			  		this.shows=false
			  		setTimeout(() => {
			  		     uni.navigateBack({
			  				 delta:1
			  			 })
			  		}, 1000);
			  	}else{
			  		this.$.ti_shi(res.msg)
			  	}
			  })
		  }
			
        }
    }
};
</script>

<style scoped lang="scss">
.title {
    width: 20%;
}
.addBtn {
    width: 686rpx;
    height: 100rpx;
    line-height: 100rpx;
    text-align: center;
    margin: 16px auto;
}
</style>
